<template>
  <h2>当前求和为：{{ sum }}</h2>
  <button @click="sum++">点我</button>
  <hr>
  <h2>当前信息为：{{ msg }}</h2>
  <button @click="msg += '!'">点我</button>
  <br>
  <h2>姓名：{{ person.name }}</h2>
  <h2>年龄：{{ person.age }}</h2>
  <h2>薪资：{{ person.job.j1.salary }}</h2>
  <button @click="person.name += '~'">修改姓名</button>
  <button @click="person.age ++ ">增长年龄</button>
  <button @click="person.job.j1.salary ++ ">张薪</button>
</template>

<script>
import {ref, reactive, watch, watchEffect} from 'vue'

export default {

  setup() {
    // 数据
    let sum = ref(0)
    let msg = ref("你好")
    let person = reactive({
      name: 'achao',
      age: 18,
      job: {
        j1: {
          salary: 100
        }
      }
    })

    // 监视
    watch(sum, (newValue, oldValue) => {
      console.log('sum的值发生了变化', newValue, oldValue)
    }, {})

    // watchEffect所指定的回调中用到的数据只要发生变化，则直接重新执行回调
    watchEffect(() => {
      const x = person.job.j1.salary
      console.log('watchEffect所指定的回调执行了',x)
    })

    // 返回一个对象
    return {
      sum, msg, person
    }
  }
}
</script>

<style scoped>

</style>
